package net.uhuijia.config;

import java.io.FileInputStream;
import java.net.URL;
import java.util.Map;

import org.yaml.snakeyaml.Yaml;


public class ProjectConfigYmlHelper {
	//public static void main(String[] args) {
	    //getValueByName("zuul.domain");	
	    //getValueByName("test");	
	    //getValueByName("test1.test2.test3");	
	//}
	 public static String getValueByName( String name ){
   	 String result = "";
   	 try {
            Yaml yaml = new Yaml();
            URL url = ProjectConfigYmlHelper.class.getClassLoader().getResource("project.yml");
            if (url != null) {
                //获取test.yaml文件中的配置数据，然后转换为obj，
                //yaml.load(new FileInputStream(url.getFile()));
                //也可以将值转换为Map
                @SuppressWarnings("unchecked")
				Map<String,Object> map =(Map<String,Object>)yaml.load(new FileInputStream(url.getFile()));
                //通过map我们取值就可以了.
                String[] split = name.split("\\.");
                if( split.length==1 ){
                	result = (String)map.get(name);
                }else if( split.length==2 ){
                	 @SuppressWarnings("unchecked")
     				 Map<String,String> oneCun = (Map<String,String>)map.get(split[0]);
                     result = oneCun.get(split[1]);
                }else if( split.length==3 ){
                	 @SuppressWarnings("unchecked")
     				 Map<String,Object> oneCun = (Map<String,Object>)map.get(split[0]);
                	 @SuppressWarnings("unchecked")
     				 Map<String,String> twoCun = (Map<String,String>)oneCun.get(split[1]);
                     result = twoCun.get(split[2]);
                }
               
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
   	 return result;
    }
}
